home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / ANTENNA / YAGIU112 / GENETIC.C% < prev    next >
Text File  |  1995-08-17  |  5KB  |  138 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include "yagi.h"
  4.  
  5. #define granularity 8 /* 2^granularity = possible different lengths */
  6.  
  7. #define LENGTH_MIN  0.35 /* min length of an element (in lambda) */
  8. #define LENGTH_MAX  0.50 /* max length of an element (in lambda) */
  9. #define SPACE_MIN   0.10 /* min spacing between any two elements (in lambda) */
  10. #define SPACE_MAX   0.42 /* max spacing between any two elements (in lambda) */
  11.  
  12. extern int popsize;
  13. extern int iterations;
  14.  
  15. double **data_driveng, **data_parasiticg, *vg, **zg, **Ag, *bg;
  16. double *ping, design_frequencyg;
  17. int driveng, parasiticg, *indxg, elementsg;
  18. struct FCOMPLEX *voltageg, *currentg, *input_impedanceg;
  19. struct element_data *coordinatesg;
  20. double lambda;
  21. struct performance_data *mean_performanceg;
  22. struct flags flagg;
  23. char *output_filenameg, *update_filenameg;
  24. double min_frequencyg, max_frequencyg, step_frequencyg, angular_stepg;
  25. int k;
  26.  
  27. void genetic_algorithm(char *output_filename, char *update_filename, struct flags flag, double design_frequency, double min_frequency, double max_frequency, double step_frequency, double angular_step, int driven, int parasitic, double **data_driven, double **data_parasitic, double *v, double **z, double *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_impedance, struct element_data *coordinates, double **A, double  *b, int *indx,struct performance_data *mean_performance) 
  28. {
  29.     double fitness;
  30.     int elements, i;
  31.     elements=driven+parasitic;
  32.     /* Set up global pointers; save modifiy GA code too much */
  33.     data_driveng=data_driven;
  34.     data_parasiticg=data_parasitic;
  35.     ping=pin; 
  36.     mean_performanceg=mean_performance;
  37.     vg=v;
  38.     zg=z;
  39.     Ag=A;
  40.     bg=b;
  41.     voltageg=voltage;
  42.     currentg=current;
  43.     elementsg=elements;
  44.     input_impedanceg=input_impedance;
  45.     coordinatesg=coordinates;
  46.     indxg=indx;
  47.     driveng=driven;
  48.     flagg=flag;
  49.     parasiticg=parasitic;
  50.     design_frequencyg=design_frequency;
  51.     min_frequencyg=min_frequency;
  52.     max_frequencyg=max_frequency;
  53.     step_frequencyg=step_frequency;
  54.     angular_stepg=angular_step;
  55.     output_filenameg=output_filename;
  56.     update_filenameg=update_filename;
  57.     if(popsize ==0) /* ie  not set by user */
  58.         popsize = 40;
  59.  
  60.     lambda = 3e8/design_frequency;    
  61.     /* Code with an granularity (eg 8) bit string the length in the range 0 to 
  62.     1, wavelength. Multiply by lambda to get true lengths.
  63.  
  64.     Likewise code spacing between individual elements as a 8 bit
  65.     string, then convert to metres by multiplying by lambda */
  66.     Initialise(popsize,2*granularity*elements-granularity);
  67.     SetPrint(0) ;
  68.     for(k=1; k<=iterations; ++k)
  69.     {
  70.         if(k%1==0)
  71.             SetPrint(0) ;
  72.         else
  73.             SetPrint(0);
  74.         end_if_stop_exists(&k,iterations,5);
  75.         Selection(stdout,k) ;
  76.     }
  77.     GA_Free();
  78. }
  79.  
  80. double Objective(char *gene) 
  81. {
  82.         int i;    
  83.         double fitness, length_max=LENGTH_MAX;
  84.         static double max_fitness=0;
  85.  
  86.         data_parasiticg[1][X]=0.0; /* Set reflector at x=0 */
  87.         /* Find the length of the reflector */
  88.         data_parasiticg[1][LENGTH]=(ss2r(gene,0,granularity)*(LENGTH_MAX-LENGTH_MIN)+LENGTH_MIN)*lambda;
  89.         for(i=1; i<=driveng; i++) /* for every driven element */
  90.         {
  91.             data_driveng[i][X]=(ss2r(gene,granularity,granularity)*(SPACE_MAX-SPACE_MIN)+SPACE_MIN)*lambda;
  92.             data_driveng[i][LENGTH]=(ss2r(gene,2*granularity,granularity)*(LENGTH_MAX-LENGTH_MIN)+LENGTH_MIN)*lambda; 
  93.  
  94.         }
  95.         /* put directors in + x direction */
  96.         for(i=2; i<=parasiticg; i++)
  97.         {
  98.             if(i==2) /* put first director ahead of the driven element */
  99.                 data_parasiticg[i][X]=data_driveng[1][X]            +(ss2r(gene,granularity*2*i-granularity,granularity)*(SPACE_MAX-SPACE_MIN)+SPACE_MIN)*lambda;  
  100.             else /* These directors must be ahead of the last director */
  101.                 data_parasiticg[i][X]=data_parasiticg[i-1][X] +(ss2r(gene,granularity*2*i-granularity,granularity)*(SPACE_MAX-SPACE_MIN)+SPACE_MIN)*lambda;
  102.             do{
  103.                 data_parasiticg[i][LENGTH]=(ss2r(gene,granularity*2*i,granularity)*(LENGTH_MAX-LENGTH_MIN) +LENGTH_MIN)*lambda;
  104.             }
  105.             while(data_parasiticg[i][LENGTH] < data_parasiticg[i-1][LENGTH] );
  106.                                 
  107.         }
  108.         fitness=get_genetic_algorithm_fitness(flagg, design_frequencyg,driveng,parasiticg,data_driveng, data_parasiticg,vg, zg, ping, voltageg, currentg, input_impedanceg, coordinatesg, Ag, bg, indxg, mean_performanceg);
  109.         if(fitness > max_fitness)
  110.         {
  111.             max_fitness=fitness;
  112.             do_since_better(k,output_filenameg, update_filenameg, *input_impedanceg,*mean_performanceg, flagg, "Optimised with the genetic algorithm", design_frequencyg, min_frequencyg,max_frequencyg,step_frequencyg, elementsg, driveng, parasiticg,angular_stepg,data_driveng,data_parasiticg,1.0, fitness); 
  113.         }
  114.  
  115. #ifdef DEBUG
  116.     if(errno)
  117.     {
  118.     fprintf(stderr,"Errno =%d in Objective() of genetic.c\n", errno);
  119.     exit(1);
  120.     }
  121. #endif
  122.         return(fitness);
  123. }
  124.  
  125. double ss2r(char *string,int pos,int len)
  126. {
  127.         double result, x ;
  128.         int loop;
  129.  
  130.         result=0 ;
  131.         for (loop=0 ; loop<len ; loop++) /* XXXX */
  132.                         result=result+result+string[pos+loop]-'0' ;     
  133.                 
  134.                 x=(result+1)/((double)(1<<granularity)); 
  135.                 return(x);
  136. }
  137.  
  138.